#include "ML_IOTools.h"
#include "ML_JointBoosting.h"
#include "ImageLoader.h"
#include "ImageFragment.h"
#include "ML_XMLParsers.h"
#include <fstream>

GLOBAL_NAMESPACE_BEGIN
NAMESPACE_MACHINE_LEARNING_BEGIN

bool MLIOTools::saveToXMLFile(const char* xmlFile, const MLJointBoosting* learner)
{
    tinyxml2::XMLDocument doc;
    tinyxml2::XMLElement* elem = MLXmlParsers::parseToXMLElement(&doc, learner);
    if (elem==NULL)
        return false;

    doc.LinkEndChild(elem);
    return doc.SaveFile(xmlFile) != tinyxml2::XML_NO_ERROR;
}

bool MLIOTools::loadFromXMLFile(const char* xmlFile, MLJointBoosting* learner)
{
    tinyxml2::XMLDocument doc;
    doc.LoadFile(xmlFile);
    return MLXmlParsers::parseFromXMLElement(doc.RootElement(), learner)!=NULL;
}

bool MLIOTools::saveToXMLFile(const char* xmlFile, const ImageLoader* imageLoader)
{
    tinyxml2::XMLDocument doc;
    tinyxml2::XMLElement* elem = MLXmlParsers::parseToXMLElement(&doc, imageLoader);
    if (elem==NULL)
        return false;

    doc.LinkEndChild(elem);
    return doc.SaveFile(xmlFile) != tinyxml2::XML_NO_ERROR;
}

bool MLIOTools::loadFromXMLFile(const char* xmlFile, ImageLoader* imageLoader)
{
    tinyxml2::XMLDocument doc;
    doc.LoadFile(xmlFile);
    return MLXmlParsers::parseFromXMLElement(doc.RootElement(), imageLoader)!=NULL;
}


NAMESPACE_MACHINE_LEARNING_END
GLOBAL_NAMESPACE_END